Click on an explanation to see the code and visualization.
peek()orgetMax()
This operation simply returns the element at the root (index 0) without modifying the heap. It has a time complexity of $O(1)$.size()
This returns the total number of elements in the heap, which is usually just the length of the underlying array. This is also an $O(1)$ operation.
heap_class.py
class MaxHeap:
def __init__(self):
self.heap = []
def peek(self):
if not self.heap:
return None
return self.heap[0]
def size(self):
return len(self.heap)